package com.sky.service.impl;

import com.sky.dto.GoodsSalesDTO;
import com.sky.entity.OrderDetail;
import com.sky.entity.Orders;
import com.sky.mapper.OrderDetailMapper;
import com.sky.mapper.OrderMapper;
import com.sky.mapper.UserMapper;
import com.sky.service.ReportService;
import com.sky.vo.OrderReportVO;
import com.sky.vo.SalesTop10ReportVO;
import com.sky.vo.TurnoverReportVO;
import com.sky.vo.UserReportVO;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.stereotype.Service;
import org.springframework.util.CollectionUtils;

import java.time.LocalDate;
import java.time.LocalDateTime;
import java.time.LocalTime;
import java.util.*;
import java.util.Map.Entry;
import java.util.stream.Collectors;

@Service
public class ReportServiceImpl implements ReportService {
    @Autowired
    private OrderMapper orderMapper;
    @Autowired
    private UserMapper userMapper;
    @Autowired
    private OrderDetailMapper orderDetailMapper;

    /**
     * 统计营业额
     *
     * @param begin
     * @param end
     * @return
     */
    public TurnoverReportVO getTurnoverStatistics(LocalDate begin, LocalDate end) {
        List<LocalDate> list = getDateList(begin, end);

        //统计营业额
        //select sum(amount) from oredrs where order_time > beginTime and order_time < endTime and status=5
        List<Double> turnoverList = new ArrayList();
        for (LocalDate date : list) {
            LocalDateTime beginTime = LocalDateTime.of(date, LocalTime.MIN);
            LocalDateTime endTime = LocalDateTime.of(date, LocalTime.MAX);

            Map map = new HashMap();
            map.put("beginTime", beginTime);
            map.put("endTime", endTime);
            map.put("status", Orders.COMPLETED);
            Double turnover = orderMapper.getSumAmount(map);

            turnover = turnover == null ? 0.0 : turnover;
            turnoverList.add(turnover);
        }

        //返回结果
        return TurnoverReportVO
                .builder()
                .dateList(StringUtils.join(list, ","))
                .turnoverList(StringUtils.join(turnoverList, ","))
                .build();
    }

    /**
     * 统计用户数据
     *
     * @param begin
     * @param end
     * @return
     */
    public UserReportVO getUserStatistics(LocalDate begin, LocalDate end) {
        //日期
        List<LocalDate> dateList = getDateList(begin, end);

        //用户总数
        List<Double> totalUserList = new ArrayList();
        List<Double> newUserList = new ArrayList();
        for (LocalDate date : dateList) {
            LocalDateTime beginTime = LocalDateTime.of(date, LocalTime.MIN);
            LocalDateTime endTime = LocalDateTime.of(date, LocalTime.MAX);

            Map map = new HashMap();
            map.put("endTime", endTime);
            Double totalUser = userMapper.getUserStatistics(map);
            totalUserList.add(totalUser);

            map.put("beginTime", beginTime);
            Double newUser = userMapper.getUserStatistics(map);
            newUserList.add(newUser);

        }

        //新增用户
        return UserReportVO
                .builder()
                .dateList(StringUtils.join(dateList, ","))
                .totalUserList(StringUtils.join(totalUserList, ","))
                .newUserList(StringUtils.join(newUserList, ","))
                .build();
    }

    /**
     * 订单统计
     *
     * @param begin
     * @param end
     * @return
     */
    public OrderReportVO getOrdersStatistics(LocalDate begin, LocalDate end) {
        //日期
        List<LocalDate> dateList = getDateList(begin, end);

        List<Integer> orderCountList = new ArrayList<>(); //每日订单数
        List<Integer> validOrderCountList = new ArrayList<>(); //每日有效订单数

        for (LocalDate date : dateList) {
            LocalDateTime beginTime = LocalDateTime.of(date, LocalTime.MIN);
            LocalDateTime endTime = LocalDateTime.of(date, LocalTime.MAX);
            Map map = new HashMap();

            //每日订单数
            map.put("endTime", endTime);
            map.put("beginTime", beginTime);
            orderCountList.add(orderMapper.countOrderByMap(map));
            //每日有效订单数
            map.put("status", Orders.COMPLETED);
            validOrderCountList.add(orderMapper.countOrderByMap(map));
        }
        Map map = new HashMap();
        //订单总数
        Integer totalOrder = orderMapper.countOrderByMap(map);
        //有效订单数
        map.put("status", Orders.COMPLETED);
        Integer validOrder = orderMapper.countOrderByMap(map);

        //新增用户
        return OrderReportVO
                .builder()
                .dateList(StringUtils.join(dateList, ","))
                .orderCountList(StringUtils.join(orderCountList, ",")) //每日订单数
                .validOrderCountList(StringUtils.join(validOrderCountList, ",")) //每日有效订单数
                .totalOrderCount(totalOrder) //订单总数
                .validOrderCount(validOrder) //有效订单数
                .orderCompletionRate((double) validOrder / totalOrder) //订单完成率
                .build();
    }

    /**
     * 查询销量排名top10
     *
     * @param begin
     * @param end
     * @return
     */
    public SalesTop10ReportVO getSalesTop10(LocalDate begin, LocalDate end) {
        //日期
        LocalDateTime beginTime = LocalDateTime.of(begin, LocalTime.MIN);
        LocalDateTime endTime = LocalDateTime.of(end, LocalTime.MAX);

        List<GoodsSalesDTO> list=orderMapper.getSalesTop10(beginTime,endTime);

        List<String> nameList = list.stream().map(g -> g.getName()).collect(Collectors.toList());
        List<Integer> numberList = list.stream().map(g -> g.getNumber()).collect(Collectors.toList());


        return SalesTop10ReportVO
                .builder()
                .nameList(StringUtils.join(nameList,","))
                .numberList(StringUtils.join(numberList,","))
                .build();
    }

    private static List<LocalDate> getDateList(LocalDate begin, LocalDate end) {
        List<LocalDate> list = new ArrayList();
        //添加日期
        list.add(begin);
        while (!begin.isEqual(end)) {
            begin = begin.plusDays(1);
            list.add(begin);
        }
        return list;
    }
}
